package cn.lefull.service;

/**
 * @description:
 * @author: qijiang
 * @date: 2019/3/19 17:40
 */

public class SynchronizedTest implements Runnable{

   // -----------------1----------------

    private int tickets = 100;
    Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                    }
                    System.out.println(Thread.currentThread().getName() + "....sale...." + tickets--);
                }
            }
        }
    }


 /*   // -----------------2----------------
    private static  int num = 100;
    //  Object obj = new Object();
    public boolean flag = true;
    @Override
    public void run(){
//      System.out.println("this:"+this.getClass());

        if(flag)
            while(true)
            {
                synchronized(SynchronizedTest.class)//(this.getClass())
                {
                    if(num>0)
                    {
                        try{Thread.sleep(10);}catch (InterruptedException e){}
                        System.out.println(Thread.currentThread().getName()+".....obj...."+num--);
                    }
                }
            }
        else
            while(true)
                this.show();
    }

    public static synchronized void show()//同步函数
    {
        if(num>0)
        {
            try{Thread.sleep(10);}catch (InterruptedException e){}

            System.out.println(Thread.currentThread().getName()+".....function...."+num--);
        }
    } */



}
